# Maintainer: Luis Martinez <luis.martinez@disroot.org>
# Contributor: Rafał Brzegowy <rafal.brzegowy@yahoo.com>

_realname=mfem
pkgbase=mingw-w64-${_realname}
pkgname=("${MINGW_PACKAGE_PREFIX}-${_realname}")
pkgver=4.8
pkgrel=1
pkgdesc='Lightweight, general, scalable C++ library for finite element methods (mingw-w64)'
arch=('any')
mingw_arch=('mingw64' 'ucrt64')
url='https://github.com/mfem/mfem'
license=('spdx:BSD-3-Clause')
depends=("${MINGW_PACKAGE_PREFIX}-gcc-libs"
         #"${MINGW_PACKAGE_PREFIX}-adios2"
         "${MINGW_PACKAGE_PREFIX}-ginkgo"
         "${MINGW_PACKAGE_PREFIX}-hypre"
         "${MINGW_PACKAGE_PREFIX}-netcdf"
         "${MINGW_PACKAGE_PREFIX}-metis"
         "${MINGW_PACKAGE_PREFIX}-mpfr"
         #$([[ ${MINGW_PACKAGE_PREFIX} == *-clang-* ]] || echo "${MINGW_PACKAGE_PREFIX}-mumps")
         "${MINGW_PACKAGE_PREFIX}-omp"
         "${MINGW_PACKAGE_PREFIX}-openblas"
         #"${MINGW_PACKAGE_PREFIX}-petsc"
         #"${MINGW_PACKAGE_PREFIX}-slepc"
         "${MINGW_PACKAGE_PREFIX}-suitesparse"
         #"${MINGW_PACKAGE_PREFIX}-sundials"
         #"${MINGW_PACKAGE_PREFIX}-superlu"
         "${MINGW_PACKAGE_PREFIX}-zlib")
makedepends=("${MINGW_PACKAGE_PREFIX}-cc"
             "${MINGW_PACKAGE_PREFIX}-cmake"
             "${MINGW_PACKAGE_PREFIX}-ninja"
             "${MINGW_PACKAGE_PREFIX}-msmpi")
source=("$url/archive/v$pkgver/${_realname}-${pkgver}.tar.gz")
sha256sums=('65472f732d273832c64b2c39460649dd862df674222c71bfa82cf2da76705052')

apply_patch_with_msg() {
  for _patch in "$@"
  do
    msg2 "Applying $_patch"
    patch -Np1 -i "${srcdir}/$_patch"
  done
}

prepare() {
  cd "${_realname}-${pkgver}"
}

build() {
  declare -a _extra_config
  if check_option "debug" "n"; then
    _extra_config+=("-DCMAKE_BUILD_TYPE=Release")
  else
    _extra_config+=("-DCMAKE_BUILD_TYPE=Debug")
  fi

  #Static Build
  mkdir -p "${srcdir}/build-${MSYSTEM}-static" && cd "${srcdir}/build-${MSYSTEM}-static"

  MSYS2_ARG_CONV_EXCL="-DCMAKE_INSTALL_PREFIX=" \
    ${MINGW_PREFIX}/bin/cmake \
      -GNinja \
      "${_extra_config[@]}" \
      -DCMAKE_INSTALL_PREFIX=${MINGW_PREFIX} \
      -DBUILD_SHARED_LIBS=OFF \
      -DMFEM_USE_LAPACK=ON \
      -DMFEM_USE_MEMALLOC=ON \
      -DMFEM_USE_METIS=ON \
      -DMFEM_USE_MPFR=ON \
      -DMFEM_USE_MPI=ON \
      -DMFEM_USE_NETCDF=ON \
      -DMFEM_USE_OPENMP=ON \
      -DMFEM_USE_SUITESPARSE=ON \
      -DMFEM_USE_SUNDIALS=OFF \
      -DMFEM_USE_GINKGO=ON \
      -DMFEM_USE_ZLIB=ON \
      -DMFEM_ENABLE_TESTING=OFF \
      -DMFEM_ENABLE_EXAMPLES=OFF \
      -DMFEM_ENABLE_MINIAPPS=OFF \
      -DBLA_VENDOR=OpenBLAS \
      -DHDF5_DIR=${MINGW_PREFIX} \
      ../${_realname}-${pkgver}

  ${MINGW_PREFIX}/bin/cmake --build .

  #Shared Build
  mkdir -p "${srcdir}/build-${MSYSTEM}-shared" && cd "${srcdir}/build-${MSYSTEM}-shared"

  MSYS2_ARG_CONV_EXCL="-DCMAKE_INSTALL_PREFIX=" \
    ${MINGW_PREFIX}/bin/cmake \
      -GNinja \
      "${_extra_config[@]}" \
      -DCMAKE_INSTALL_PREFIX=${MINGW_PREFIX} \
      -DBUILD_SHARED_LIBS=ON \
      -DMFEM_USE_LAPACK=ON \
      -DMFEM_USE_MEMALLOC=ON \
      -DMFEM_USE_METIS=ON \
      -DMFEM_USE_MPFR=ON \
      -DMFEM_USE_MPI=ON \
      -DMFEM_USE_NETCDF=ON \
      -DMFEM_USE_OPENMP=ON \
      -DMFEM_USE_SUITESPARSE=ON \
      -DMFEM_USE_SUNDIALS=OFF \
      -DMFEM_USE_GINKGO=ON \
      -DMFEM_USE_ZLIB=ON \
      -DMFEM_ENABLE_TESTING=OFF \
      -DMFEM_ENABLE_EXAMPLES=OFF \
      -DMFEM_ENABLE_MINIAPPS=OFF \
      -DBLA_VENDOR=OpenBLAS \
      -DHDF5_DIR=${MINGW_PREFIX} \
      ../${_realname}-${pkgver}

  ${MINGW_PREFIX}/bin/cmake --build .
}

package() {
  #Static Install
  cd "${srcdir}/build-${MSYSTEM}-static"
  DESTDIR="${pkgdir}" ${MINGW_PREFIX}/bin/cmake --install .

  #Shared Install
  cd "${srcdir}/build-${MSYSTEM}-shared"
  DESTDIR="${pkgdir}" ${MINGW_PREFIX}/bin/cmake --install .

  install -Dm644 "${srcdir}"/${_realname}-${pkgver}/LICENSE "${pkgdir}"${MINGW_PREFIX}/share/licenses/${_realname}/LICENSE
  install -Dm644 "${srcdir}"/${_realname}-${pkgver}/README.md "${pkgdir}"${MINGW_PREFIX}/share/licenses/${_realname}/README
  install -Dm644 "${srcdir}"/${_realname}-${pkgver}/NOTICE "${pkgdir}"${MINGW_PREFIX}/share/licenses/${_realname}/NOTICE

  local PREFIX_WIN=$(cygpath -wm ${MINGW_PREFIX})
  for _f in $(find "${pkgdir}${MINGW_PREFIX}"/lib/cmake -type f); do
    sed -e "s|${PREFIX_WIN}|\$\{_IMPORT_PREFIX\}|g" -i ${_f}
    sed -e "s|${MINGW_PREFIX}|\$\{_IMPORT_PREFIX\}|g" -i ${_f}
  done
}
